home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / oleo-1_4.lha / oleo-1.4 / sc.c < prev    next >
C/C++ Source or Header  |  1993-03-30  |  6KB  |  248 lines

  1. /*    Copyright (C) 1990, 1992, 1993 Free Software Foundation, Inc.
  2.  
  3. This file is part of Oleo, the GNU Spreadsheet.
  4.  
  5. Oleo is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. Oleo is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with Oleo; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. #include "funcdef.h"
  20. #include <stdio.h>
  21. #include <ctype.h>
  22. #include "sysdef.h"
  23. #include "global.h"
  24. #include "cell.h"
  25. #include "io-generic.h"
  26. #include "io-abstract.h"
  27. #include "io-utils.h"
  28. #include "lists.h"
  29. #include "ref.h"
  30. #include "parse.h"
  31. #include "regions.h"
  32. #include "cmd.h"
  33.  
  34.  
  35.  
  36. /* This reads/writes a subset of the SC public-domain spreadsheet's
  37.    file-format.  Note that since SC has no way of encoding some information
  38.    about a cell, writing a spread out in SC format, then reading it back in
  39.    will result in the loss of some information (most important:  cell formats)
  40.  */
  41.  
  42. static int
  43. get_range (pp,rp)
  44.      char ** pp;
  45.       struct rng * rp;
  46. {
  47.     int byte;
  48.     char *p;
  49.     struct var *v;
  50.  
  51.  
  52.     while(isspace(**pp))
  53.         (*pp)++;
  54.     byte=parse_cell_or_range(pp,rp);
  55.     if(byte)
  56.         return 0;
  57.     for(p= *pp;*p && !isspace(*p);p++)
  58.         ;
  59.     v=find_var(*pp,p-*pp);
  60.     if(!v)
  61.         return 1;
  62.     *pp=p;
  63.     *rp=v->v_rng;
  64.     return 0;
  65. }
  66.  
  67. void
  68. sc_read_file (fp,ismerge)
  69.      FILE * fp;
  70.       int ismerge;
  71. {
  72.     char buf[2048];
  73.     int lineno;
  74.     char *ptr;
  75.     int n;
  76.     struct rng rng;
  77.     int olda0;
  78.  
  79.     olda0=a0;
  80.     a0=1;
  81.     lineno=0;
  82.     if(!ismerge)
  83.         clear_spreadsheet();
  84.     while(fgets(buf,sizeof(buf),fp)) {
  85.         lineno++;
  86.         if(lineno%50==0)
  87.             io_info_msg("Line %d",lineno);
  88.         if(buf[0]=='#' || buf[0]=='\n')
  89.             continue;
  90.         if(!strncmp(buf,"set ",4)) {
  91.             /* ... */
  92.         } else if(!strncmp(buf,"format ",7)) {
  93.             ptr=buf+7;
  94.             if(get_range(&ptr,&rng))
  95.                 continue;
  96.             n=astol(&ptr);
  97.             set_width(rng.lc,n);
  98.         } else if(!strncmp(buf,"hide ",5)) {
  99.             ptr=buf+5;
  100.             if(get_range(&ptr,&rng))
  101.                 continue;
  102.             set_width(rng.lc,0);
  103.         } else if(!strncmp(buf,"mdir ",5)) {
  104.             /* ... */
  105.         } else if(!strncmp(buf,"define ",7)) {
  106.             char *eptr;
  107.  
  108.             ptr=buf+7;
  109.             while(isspace(*ptr))
  110.                 ptr++;
  111.             if(*ptr!='"') {
  112.                 io_error_msg("Line %d: No starting \" in define",lineno);
  113.                 continue;
  114.             }
  115.             ptr++;
  116.             for(eptr=ptr; *eptr && *eptr!='"';eptr++)
  117.                 ;
  118.             if(!*eptr) {
  119.                 io_error_msg("Line %d: No starting \" in define",lineno);
  120.                 continue;
  121.             }
  122.             ptr=new_var_value(ptr,eptr-ptr,eptr+1);
  123.             if(ptr)
  124.                 io_error_msg("Line %d: %s",ptr);
  125.         } else if(!strncmp(buf,"leftstring ",11) ||
  126.               !strncmp(buf,"rightstring ",12)) {
  127.             CELL *cp;
  128.  
  129.             ptr=buf+11;
  130.             if(get_range(&ptr,&rng))
  131.                 continue;
  132.             while(isspace(*ptr))
  133.                 ptr++;
  134.             if(*ptr=='=')
  135.                 ptr++;
  136.             new_value(rng.lr,rng.lc,ptr);
  137.             cp=find_cell(rng.lr,rng.lc);
  138.             if(buf[0]=='l')
  139.                 SET_JST(cp,JST_LFT);
  140.             else
  141.                 SET_JST(cp,JST_RGT);
  142.  
  143.         } else if(!strncmp(buf,"let ",4)) {
  144.             ptr=buf+4;
  145.             if(get_range(&ptr,&rng))
  146.                 continue;
  147.             while(isspace(*ptr))
  148.                 ptr++;
  149.             if(*ptr=='=')
  150.                 ptr++;
  151.             new_value(rng.lr,rng.lc,ptr);
  152.         } else
  153.             io_error_msg("Line %d: Can't parse %s",lineno,buf);
  154.     }
  155.     a0=olda0;
  156.     io_recenter_all_win();
  157. }
  158.  
  159. static FILE *sc_fp;
  160. static struct rng *sc_rng;
  161. static void
  162. sc_write_var (name,var)
  163.      char * name;
  164.       struct var * var;
  165. {
  166.     if(var->var_flags==VAR_UNDEF && (!var->var_ref_fm || var->var_ref_fm->refs_used==0))
  167.         return;
  168.     switch(var->var_flags) {
  169.     case VAR_UNDEF:
  170.         break;
  171.     case VAR_CELL:
  172.         if(var->v_rng.lr>=sc_rng->lr && var->v_rng.lr<=sc_rng->hr && var->v_rng.lc>=sc_rng->lc && var->v_rng.lc<=sc_rng->hc)
  173.             (void)fprintf(sc_fp,"define \"%s\" %s\n",var->var_name,cell_name(var->v_rng.lr,var->v_rng.lc));
  174.         break;
  175.     case VAR_RANGE:
  176.         if(var->v_rng.lr<sc_rng->lr || var->v_rng.hr>sc_rng->hr || var->v_rng.lc<sc_rng->lc || var->v_rng.hc>sc_rng->hc)
  177.             break;
  178.  
  179.         (void)fprintf(sc_fp,"define \"%s\" %s\n",var->var_name,range_name(&(var->v_rng)));
  180.         break;
  181. #ifdef TEST
  182.     default:
  183.         panic("Unknown var type %d",var->var_flags);
  184.         break;
  185. #endif
  186.     }
  187. }
  188.     
  189. void
  190. sc_write_file (fp,rng)
  191.      FILE * fp;
  192.       struct rng * rng;
  193. {
  194.     unsigned short w;
  195.     CELLREF r,c;
  196.     CELL *cp;
  197.     char *ptr;
  198.     int olda0;
  199.  
  200.     if(!rng)
  201.         rng= &all_rng;
  202.  
  203.     olda0=a0;
  204.     a0=1;
  205.     (void)fprintf(fp,"# This file was created by Oleo, for use by the Spreadsheet Calculator\n");
  206.     (void)fprintf(fp,"# You probably don't want to edit it.\n\n");
  207.  
  208.     find_widths(rng->lc,rng->hc);
  209.     while(w=next_width(&c))
  210.         fprintf(fp,"format %s %d ???\n",cell_name(MIN_ROW,c),w);
  211.     sc_fp=fp;
  212.     sc_rng=rng;
  213.     for_all_vars(sc_write_var);
  214.     find_cells_in_range(rng);
  215.     while(cp=next_row_col_in_range(&r,&c)) {
  216.         switch(GET_TYP(cp)) {
  217.         case TYP_STR:
  218.             if((GET_JST(cp)==JST_DEF && default_jst==JST_RGT) || GET_JST(cp)==JST_RGT)
  219.                 ptr="right";
  220.             else ptr="left";
  221.             fprintf(fp,"%sstring %s = %s\n",ptr,cell_name(r,c),decomp(r,c,cp));
  222.             decomp_free();
  223.             break;
  224.         case 0:
  225.             break;
  226.         default:
  227.             fprintf(fp,"let %s = %s\n",cell_name(r,c),decomp(r,c,cp));
  228.             decomp_free();
  229.             break;
  230.         }
  231.     }
  232.     a0=olda0;
  233. }
  234.  
  235. int
  236. sc_set_options (set_opt,option)
  237.      int set_opt;
  238.       char * option;
  239. {
  240.     return -1;
  241. }
  242.  
  243. void
  244. sc_show_options ()
  245. {
  246.     io_text_line("File format: sc  (Public domain spreadsheet calculator)");
  247. }
  248.